//
//  CNAppDelegate.m
//  chuiniu
//
//  Created by zheng ji on 14-7-22.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "CNAppDelegate.h"
#import "common/CommonConfigUtil.h"
#import "AppDelegateAttachment.h"
#import "CNIntroduceViewController.h"
#import "CNViewController.h"
#import "CNGameUtil.h"
@interface CNAppDelegate ()
{
  
    NSDictionary *_pushPayload;
    NSUserDefaults *_useDefaults;
    AppDelegateAttachment* appAttachement;
}
@end

@implementation CNAppDelegate

/*创建存放本地金币记录的路径*/
-(void)createLocalGoldDirectory{
       BOOL isDir;
     NSFileManager *fileManager=[NSFileManager defaultManager];
    NSString *filestorePath=[CNGameUtil getLocalGoldPath];
    if(![fileManager fileExistsAtPath:filestorePath isDirectory:&isDir]){
        [fileManager createDirectoryAtPath:filestorePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.gameStatus =GameStatusNormal;
    [UMSocialData setAppKey:[CommonConfigUtil getUmengAppKey]];
    
      [NSThread detachNewThreadSelector:@selector(createLocalGoldDirectory) toTarget:self withObject:nil];
    

    
    _useDefaults=[NSUserDefaults standardUserDefaults];
    appAttachement=[[AppDelegateAttachment alloc]init];
    [appAttachement umengStartup];
    
    //[appAttachement createLocalDirectory];
    
    [appAttachement registerForRemoteNotification];
    
    
    //[appAttachement addMobileUser];
    
    [appAttachement parseLaunchingOptions:launchOptions];
    
    //
    [NSThread detachNewThreadSelector:@selector(connectToServer) toTarget:self withObject:nil];
    
    
    UIStoryboard* storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
    
    NSString* strFirstRun=[userDefaults objectForKey:@"FirstRun"];
   
    if(strFirstRun ==nil || [strFirstRun isEqualToString:@""]){
         
        //第一次运行
        CNIntroduceViewController* ctrl=(CNIntroduceViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"CNIntroduceViewController"];
        
        [userDefaults setObject:@"1" forKey:@"FirstRun"];
        [userDefaults synchronize];
        
        self.window.rootViewController=ctrl;
    }else{
       // CNMainViewController* viewCtrl=(CNMainViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"CNMainViewController"];
           UINavigationController* viewCtrl=(UINavigationController*)[storyBoard instantiateViewControllerWithIdentifier:@"MainNaviController"];
        
        self.window.rootViewController=viewCtrl;

    }
    
    return YES;
}
-(void)checkVersion{
    [CommonConfigUtil checkVersion];
}
							
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    self.gameStatus=GameStatusInterrupt;
    if(self.gameClient !=nil){
        [self.gameClient sendQuitGameCommand];//退出登录
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if(self.gameStatus== GameStatusInterrupt){
         self.gameStatus=GameStatusNormal;
        [NSThread detachNewThreadSelector:@selector(connectToServer) toTarget:self withObject:nil];
    }
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    
    // [NSThread detachNewThreadSelector:@selector(checkVersion) toTarget:self withObject:nil];

    
}

-(void)connectToServer{
    if(self.gameClient ==nil){
        self.gameClient=[[CNGameClient alloc]init];
        
    }
    if(!self.gameClient.isConnected){
     [self.gameClient connectToServer];
    }
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}
@end
